package com.aier.school.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.ClassStudent;
import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.common.SchoolAttendanceUtil;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.entity.UserIcardRecord;
import com.aier.school.service.ISchoolIcCardService;
import com.aier.school.service.ISchoolTeacherService;

@Scope("prototype")
@Component("schoolIccardRecordAction")
public class SchoolIccardRecordAction extends SchoolBaseAction {

    /**
     * 学生进出园记录
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "classService")
    private IClassService classService;
    
    @Resource(name = "schoolIcCardService")
    private ISchoolIcCardService schoolIcCardService;    
    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;
    
    private List<Classes> schoolClassList;        //学校班级列表(供下拉选择)
    private List<UserIcardRecord> stuIcardInfList;//进出园记录列表

    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    private Integer sid;            //学校id
    private Long cid;                //班级id
    private String dayValue;     //日期
    
    private int page;                //页数
    private int page_size;            //每页的记录数
    private int page_count;            //总页数
    private int count;                //总数
    
    private List<String> tnameAccountList;        //教师名字账号列表
    private String account; // 教师帐号
    private Integer type;   // 查询方式(1所有教师每日2个人每月)
    private Integer timeType;   // 时间记录查询类型(1.班级时间记录2.教师端所有人每日3.教师端每人每月)
    private String tname;   // 教师姓名
    private ByteArrayInputStream inputExcel;
    private String filename;
    
    private List<ClassStudent> csList;
    private List<String> ctList;  //时间列表
   
	private Long uid;  // 用户id
    private Long tid;  // 教师id
    
    Map<String, Object> am_in = new HashMap<String, Object>();
    Map<String, Object> am_out = new HashMap<String, Object>();
    Map<String, Object> pm_in = new HashMap<String, Object>();
    Map<String, Object> pm_out = new HashMap<String, Object>();
    private List<SchoolTeacher> stList;
    
    @Override
    public String execute() {
        schoolClassList = classService.searchClassList(sid);
        tnameAccountList = schoolTeacherService.searchTNameAndAccountBySid(sid);
        tnameAccountList.add(0, "所有教师");
        return SUCCESS;
    }
    
    /**
     * 获取时间记录列表  
     * @author zhou 2014-05-23
     * @return String
     */
    public String searchTimeList(){
    	ctList=schoolIcCardService.SearchClassList(timeType);
    	s=1;
		return SUCCESS;
    }
    
    
   

	/**
     * 查询学生打卡记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchUIIList(){
        s = 0;
        if(page<=0){
            page=1;
        }
        if(page_size<=0){
            page_size = Constant.num_10;
        }

        Map<String,Object> map = schoolIcCardService.searchStuIcardInfoListByCondation(page, page_size, 
                sid, cid, dayValue);
        if(null!=map){
            stuIcardInfList = (List<UserIcardRecord>) map.get(ICommonDAO.OBJ_LIST);
            if (null != stuIcardInfList) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            
        }
        return SUCCESS;
    }
    
    /**
     * 教师打卡记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchTIIList(){
        s = 0;
        if(page<=0){
            page=1;
        }
        if(page_size<=0){
            page_size = Constant.num_10;
        }
        Map<String,Object> map;
        if (type == null ||type == 1 ) {
            map = schoolIcCardService.searchTeaIcardInfoListByDay(page, page_size, 
                    sid, dayValue);
        } else {
            map = schoolIcCardService.searchTeaIcardInfoListByMonth(page, page_size, 
                    dayValue,account,sid);
        }
        
        if(null!=map){
            stuIcardInfList = (List<UserIcardRecord>) map.get(ICommonDAO.OBJ_LIST);
            if (null != stuIcardInfList) {
                s = 1;
            }
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT);
            
        }
        return SUCCESS;
    }
    
    public String down() {
        
        if (type == null ||type == 1 ) {
            stuIcardInfList = schoolIcCardService.searchTeaIcardInfoListByDay(sid, dayValue);
            filename="teacher_iccard_"+dayValue+".xls";
        } else {
            stuIcardInfList = schoolIcCardService.searchTeaIcardInfoListByMonth(dayValue,account,sid);
            filename=tname+"_"+dayValue+".xls";
        }
        try {
            filename = URLEncoder.encode(filename, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } 
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        
        try {
            SchoolAttendanceUtil.createTIIExcel(stuIcardInfList,type).write(out);
        } catch (IOException e) {
            return null;
        } catch (Exception e){
            e.printStackTrace();
        }
        inputExcel = new ByteArrayInputStream(out.toByteArray());
        //filename="teacher_iccard_info.xls";
        return SUCCESS;        
    }
    
    /**
     * 查询某班级家长的学生列表
     * @return
     */
    public String searchCSByParent() {
        csList = schoolIcCardService.searchCSByParent(uid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查询学校的班级列表
     * @return
     */
    public String searchClassesBySid() {
        schoolClassList = classService.searchClassList(sid);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 查询自己的打卡记录(学生或教师)
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchIccardRecordSelf() {
        s = 0;
        if(page<=0){
            page=1;
        }
        if(page_size<=0){
            page_size = Constant.num_10;
        }
        Map<String,Object> map = schoolIcCardService.searchSelfIcardInfoList(page, page_size, 
                    uid, tid);
        
        if(null!=map){
            stuIcardInfList = (List<UserIcardRecord>) map.get(ICommonDAO.OBJ_LIST);
            page = (Integer) map.get(ICommonDAO.PAGE);
            page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
            page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
            count = (Integer) map.get(ICommonDAO.COUNT); 
        }
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 学生打卡记录（app端）
     * @return
     */
    public String searchCSRecordList() {
        s = 1;
        List<UserIcardRecord> uirList = schoolIcCardService.searchsearchCSRecordList(cid, dayValue);
        count = uirList.size();
        // 上午进园
        List<String> unpunchList = new ArrayList<String>();
        List<Map<String,String>> punchList = new ArrayList<Map<String,String>>();
        for (UserIcardRecord uir : uirList) {
            if (uir.getAm_in().equals("未打卡")) {
                unpunchList.add(uir.getName());
            } else if (!uir.getAm_in().equals("未打卡")){
                Map<String,String> map = new HashMap<String,String>();
                map.put("name", uir.getName());
                map.put("time", uir.getAm_in());
                punchList.add(map);
            }
        }
        am_in.put("unpunchList", unpunchList);
        am_in.put("punchList", punchList);
        am_in.put("unCount", unpunchList.size());
        // 上午出园
        unpunchList = new ArrayList<String>();
        punchList = new ArrayList<Map<String,String>>();
        for (UserIcardRecord uir : uirList) {
            if (uir.getAm_out().equals("未打卡")) {
                unpunchList.add(uir.getName());
            } else if (!uir.getAm_out().equals("未打卡")){
                Map<String,String> map = new HashMap<String,String>();
                map.put("name", uir.getName());
                map.put("time", uir.getAm_out());
                punchList.add(map);
            }
        }
        am_out.put("unpunchList", unpunchList);
        am_out.put("punchList", punchList);
        am_out.put("unCount", unpunchList.size());
        // 下午进园
        unpunchList = new ArrayList<String>();
        punchList = new ArrayList<Map<String,String>>();
        for (UserIcardRecord uir : uirList) {
            if (uir.getPm_in().equals("未打卡")) {
                unpunchList.add(uir.getName());
            } else if (!uir.getPm_in().equals("未打卡")){
                Map<String,String> map = new HashMap<String,String>();
                map.put("name", uir.getName());
                map.put("time", uir.getPm_in());
                punchList.add(map);
            }
        }
        pm_in.put("unpunchList", unpunchList);
        pm_in.put("punchList", punchList);
        pm_in.put("unCount", unpunchList.size());
        // 下午出园
        unpunchList = new ArrayList<String>();
        punchList = new ArrayList<Map<String,String>>();
        for (UserIcardRecord uir : uirList) {
            if (uir.getPm_out().equals("未打卡")) {
                unpunchList.add(uir.getName());
            } else if (!uir.getPm_out().equals("未打卡")){
                Map<String,String> map = new HashMap<String,String>();
                map.put("name", uir.getName());
                map.put("time", uir.getPm_out());
                punchList.add(map);
            }
        }
        pm_out.put("unpunchList", unpunchList);
        pm_out.put("punchList", punchList);
        pm_out.put("unCount", unpunchList.size());
        
        return SUCCESS;
    }
    
    /**
     * 查询学校教师列表
     * @return
     */
    public String searchTeacherList() {
        s =1;
        stList = schoolTeacherService.selectTeacher(sid,null,null);
        return SUCCESS;
    }
    
  
    
    /**
     * 教师打卡记录(app端)
     * @return
     */
    public String searchTIIListApp(){
        s = 1;
        if (type == null ||type == 1 ) {
            List<UserIcardRecord> uirList = schoolIcCardService.searchTeaIcardInfoListByDay(sid, dayValue);
            count = uirList.size();
            // 上午进园
            List<String> unpunchList = new ArrayList<String>();
            List<Map<String,String>> punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (uir.getAm_in().equals("未打卡")) {
                    unpunchList.add(uir.getName());
                } else if (!uir.getAm_in().equals("未打卡")){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getAm_in());
                    punchList.add(map);
                }
            }
            am_in.put("unpunchList", unpunchList);
            am_in.put("punchList", punchList);
            am_in.put("unCount", unpunchList.size());
            // 上午出园
            unpunchList = new ArrayList<String>();
            punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (uir.getAm_out().equals("未打卡")) {
                    unpunchList.add(uir.getName());
                } else if (!uir.getAm_out().equals("未打卡")){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getAm_out());
                    punchList.add(map);
                }
            }
            am_out.put("unpunchList", unpunchList);
            am_out.put("punchList", punchList);
            am_out.put("unCount", unpunchList.size());
            // 下午进园
            unpunchList = new ArrayList<String>();
            punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (uir.getPm_in().equals("未打卡")) {
                    unpunchList.add(uir.getName());
                } else if (!uir.getPm_in().equals("未打卡")){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getPm_in());
                    punchList.add(map);
                }
            }
            pm_in.put("unpunchList", unpunchList);
            pm_in.put("punchList", punchList);
            pm_in.put("unCount", unpunchList.size());
            // 下午出园
            unpunchList = new ArrayList<String>();
            punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (uir.getPm_out().equals("未打卡")) {
                    unpunchList.add(uir.getName());
                } else if (!uir.getPm_out().equals("未打卡")){
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getPm_out());
                    punchList.add(map);
                }
            }
            pm_out.put("unpunchList", unpunchList);
            pm_out.put("punchList", punchList);
            pm_out.put("unCount", unpunchList.size());
        } else {
            List<UserIcardRecord> uirList = schoolIcCardService.searchTeaIcardInfoListByMonthTid(dayValue,tid);
            // 上午进园
            List<Map<String,String>> punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (!uir.getAm_in().equals("未打卡")) {
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getAm_in());
                    punchList.add(map);
                }
            }
            am_in.put("punchList", punchList);
            // 上午出园
            punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (!uir.getAm_out().equals("未打卡")) {
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getAm_out());
                    punchList.add(map);
                }
            }
            am_out.put("punchList", punchList);
            // 下午进园
            punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (!uir.getPm_in().equals("未打卡")) {
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getPm_in());
                    punchList.add(map);
                }
            }
            pm_in.put("punchList", punchList);
            // 下午出园
            punchList = new ArrayList<Map<String,String>>();
            for (UserIcardRecord uir : uirList) {
                if (!uir.getPm_out().equals("未打卡")) {
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("name", uir.getName());
                    map.put("time", uir.getPm_out());
                    punchList.add(map);
                }
            }
            pm_out.put("punchList", punchList);
            
        }
        
        return SUCCESS;
    }
    
    
    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public List<Classes> getSchoolClassList() {
        return schoolClassList;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setSchoolIcCardService(ISchoolIcCardService schoolIcCardService) {
        this.schoolIcCardService = schoolIcCardService;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }
    
    public void setDayValue(String dayValue) {
        this.dayValue = dayValue;
    }

    public List<UserIcardRecord> getStuIcardInfList() {
        return stuIcardInfList;
    }

    public List<String> getTnameAccountList() {
        return tnameAccountList;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public ByteArrayInputStream getInputExcel() {
        return inputExcel;
    }

    public void setInputExcel(ByteArrayInputStream inputExcel) {
        this.inputExcel = inputExcel;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public List<ClassStudent> getCsList() {
        return csList;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public Map<String, Object> getAm_in() {
        return am_in;
    }

    public Map<String, Object> getAm_out() {
        return am_out;
    }

    public Map<String, Object> getPm_in() {
        return pm_in;
    }

    public Map<String, Object> getPm_out() {
        return pm_out;
    }

    public List<SchoolTeacher> getStList() {
        return stList;
    }
    public List<String> getCtList() {
		return ctList;
	}
    
    public void setTimeType(Integer timeType) {
    	this.timeType = timeType;
	}

}
